/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.scalia.meteocal.weather;

import com.scalia.meteocal.entity.Event;
import com.scalia.meteocal.entity.WeatherType;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 *
 * @author gabriele
 */
public class StubForecastProvider implements ForecastProvider {

    private static final int DAY_BEFORE_AVAIABILITY = 3;
    private static final int ADJUSTE = -10;
    
    @Override
    public WeatherForecast getForecast(String city, Date date) {
        Date now = new Date();
        if (date.getTime() - now.getTime() > 1000*60*60*24*DAY_BEFORE_AVAIABILITY) {
            return null; //the forecast is not avaiable yet
        }
        
        DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");      
        long seed = city.hashCode() + formatter.format(date).hashCode(); //get date without hour/minute/seconds
        
        Random random = new Random(seed);
        float forecastTemperature = random.nextInt(Event.MAX_TEMPERATURE_AVAIABLE - Event.MIN_TEMPERATURE_AVAIABLE) + Event.MIN_TEMPERATURE_AVAIABLE + ADJUSTE;
        float decimal = random.nextFloat();
        String cutted = String.valueOf(decimal).substring(0, 3);
        decimal = Float.parseFloat(cutted);
        forecastTemperature+=decimal;
        WeatherType weatherType = WeatherType.values()[random.nextInt(WeatherType.values().length)];
        
        return new WeatherForecast(weatherType, forecastTemperature);
    }
    
}
